#!/bin/bash

KERNEL_VERSION="$(uname -r)"
CURRENT_DIR="${ROOT_DIR}/driver"
DRIVER_JH_DIR="${CURRENT_DIR}/driver_jh"
DRIVER_TYC_DIR="${CURRENT_DIR}/driver_tyc"
JH_INSTALL_DIR="${PACKAGE_DIR}/lib/modules/${KERNEL_VERSION}/kernel/drivers/watchdog"
TYC_INSTALL_DIR="${PACKAGE_DIR}/lib/modules/${KERNEL_VERSION}"

build_driver_jh() {
    echo "Building driver_jh..."
    if [ -d "${DRIVER_JH_DIR}" ]; then
        cd "${DRIVER_JH_DIR}"
        if [ -f "Makefile" ]; then
            make clean
            make
            if [ $? -ne 0 ]; then
                echo "compile driver failure"
                exit 1
            fi

            mkdir -p "${JH_INSTALL_DIR}"
            \cp -f sbsa_gwdt.ko "${JH_INSTALL_DIR}"
        else
            echo "Makefile not found in ${DRIVER_JH_DIR}."
        fi
        cd -
    else
        echo "${DRIVER_JH_DIR} not found."
    fi
}

build_driver_tyc() {
    echo "Building driver_tyc..."
    if [ -d "${DRIVER_TYC_DIR}" ]; then
        cd "${DRIVER_TYC_DIR}"
        if [ -f "Makefile" ]; then
            make clean
            make
            if [ $? -ne 0 ]; then
                echo "compile driver failure"
                exit 1
            fi

            mkdir -p "${TYC_INSTALL_DIR}"
            \cp -f ft_wdt.ko "${TYC_INSTALL_DIR}"
        else
            echo "Makefile not found in ${DRIVER_TYC_DIR}."
        fi
        cd -
    else
        echo "${DRIVER_TYC_DIR} not found."
    fi
}

# 主函数
main() {
    echo "Starting build driver process..."
    build_driver_jh
    build_driver_tyc
    echo "Build driver process completed."
}

# 执行主函数
main
